package com.das.mechanic_base.gen;

import android.util.Log;

import com.das.mechanic_base.app.X3MyApp;
import com.das.mechanic_base.bean.alone.AloneImageBean;
import com.das.mechanic_base.bean.alone.AloneItemCommentBean;
import com.das.mechanic_base.bean.alone.AloneSearchBean;
import com.das.mechanic_base.bean.custrecord.CustomRecordListBean;
import com.das.mechanic_base.bean.greendao.AddTagBean;
import com.das.mechanic_base.bean.main.CarMemberBean;
import com.das.mechanic_base.bean.main.MineRecordBean;
import com.das.mechanic_base.bean.search.SaveSearchBean;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：cs on 2019/10/11 17:15
 * <p>
 * 邮箱：916256628@qq.com
 */
public class DaoSessionUtils {

    static DaoSession daoSession;

    private SaveSearchBeanDao searchBeanDao;
    private MineRecordBeanDao recordBeanDao;
    private AloneImageBeanDao imageBeanDao;
    private CustomRecordListBeanDao recordListBeanDao;
    private AloneSearchBeanDao aloneSearchBeanDao;
    private WorkNewImageBeanDao workNewImageBeanDao;
    private WorkProcessEntityBeanDao workProcessEntityBeanDao;
    private MethodTransferBeanDao methodTransferBeanDao;
    private AloneItemCommentBeanDao aloneItemCommentBeanDao;


    private static DaoSessionUtils instance;
    private final CarMemberBeanDao carMemberBeanDao;

    public static DaoSession getDaoInstance() {

        if (daoSession == null) {
            daoSession = X3MyApp.getInstance().getDaoSession();
        }
        //清空所有数据表的缓存数据
        //daoSession.clear();
        return daoSession;
    }

    private DaoSessionUtils() {
        searchBeanDao = X3MyApp.getInstance().getDaoSession().getSaveSearchBeanDao();
        aloneSearchBeanDao = X3MyApp.getInstance().getDaoSession().getAloneSearchBeanDao();
        recordBeanDao = X3MyApp.getInstance().getDaoSession().getMineRecordBeanDao();
        imageBeanDao = X3MyApp.getInstance().getDaoSession().getAloneImageBeanDao();
        recordListBeanDao = X3MyApp.getInstance().getDaoSession().getCustomRecordListBeanDao();
        workNewImageBeanDao = X3MyApp.getInstance().getDaoSession().getWorkNewImageBeanDao();
        carMemberBeanDao = X3MyApp.getInstance().getDaoSession().getCarMemberBeanDao();
        workProcessEntityBeanDao = X3MyApp.getInstance().getDaoSession().getWorkProcessEntityBeanDao();
        methodTransferBeanDao = X3MyApp.getInstance().getDaoSession().getMethodTransferBeanDao();
        aloneItemCommentBeanDao = X3MyApp.getInstance().getDaoSession().getAloneItemCommentBeanDao();
    }

    public static DaoSessionUtils getInstance() {
        if (instance == null) {
            synchronized (DaoSessionUtils.class) {
                if (instance == null) {
                    instance = new DaoSessionUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 插入施工报告中自定义一条工序
     * @param
     */
    public void insertWorkProcess(WorkProcessEntityBean entityBean) {
        workProcessEntityBeanDao.detachAll();
        workProcessEntityBeanDao.insert(entityBean);
    }

    /**
     * 更新施工报告中自定义一条工序
     * @param entityBean
     */
    public void updateWorkProcess(WorkProcessEntityBean entityBean) {
        workProcessEntityBeanDao.detachAll();
        workProcessEntityBeanDao.update(entityBean);
        workProcessEntityBeanDao.refresh(entityBean);
    }

    /**
     * 自定义工序
     * 查询目前为施工报告中自定义工序
     *
     * @return
     */
    public WorkProcessEntityBean searchWorkProcessEntityBean(String workKey) {
        workProcessEntityBeanDao.detachAll();
        WorkProcessEntityBean entityBean = workProcessEntityBeanDao.queryBuilder().where(WorkProcessEntityBeanDao.Properties.WorkKey.eq(workKey)).build().unique();
        return entityBean;
    }

    /**
     * 插入一条施工报告数据
     *
     * @param
     */
    public void insertWorkNewBean(WorkNewImageBean imageBean) {
        workNewImageBeanDao.detachAll();
        workNewImageBeanDao.insert(imageBean);
    }

    /**
     * 删除施工报告数据
     */
    public void deleteWorkNewBean(WorkNewImageBean imageBean) {
        workNewImageBeanDao.detachAll();
        workNewImageBeanDao.delete(imageBean);
    }

    /**
     * 更新施工报告数据
     */
    public void updateWorkNewBean(WorkNewImageBean listBean) {
        workNewImageBeanDao.detachAll();
        workNewImageBeanDao.update(listBean);
        workNewImageBeanDao.refresh(listBean);
    }

    /**
     * 自定义工序
     * 查询目前为止施工报告数据
     *
     * @return
     */
    public WorkNewImageBean searchWorkNewBean(String workKey) {
        workNewImageBeanDao.detachAll();
        WorkNewImageBean imageBean = workNewImageBeanDao.queryBuilder().where(WorkNewImageBeanDao.Properties.WorkKey.eq(workKey)).build().unique();
        return imageBean;
    }

    /**
     * 自定义工序
     * 查询目前为止施工报告数据
     *
     * @return
     */
    public List<WorkNewImageBean> searchProcessBean(String processKey) {
        workNewImageBeanDao.detachAll();
        List<WorkNewImageBean> imageBean = workNewImageBeanDao.queryBuilder().where(WorkNewImageBeanDao.Properties.ProcessKey.eq(processKey)).build().list();
        return imageBean;
    }

    /**
     * 自定义工序
     * 删除工序后更新数据
     *
     * @return
     */
    public List<WorkNewImageBean> updateProcessBean(String processKey) {
        workNewImageBeanDao.detachAll();
        List<WorkNewImageBean> imageBean = workNewImageBeanDao.queryBuilder().where(WorkNewImageBeanDao.Properties.ProcessKey.eq(processKey)).build().list();
        return imageBean;
    }

    /**
     * 查询该施工单下的所有数据
     *
     * @return
     */
    public List<WorkNewImageBean> searchWorkNewBeanById(long workBaseId) {
        workNewImageBeanDao.detachAll();
        List<WorkNewImageBean> imageBean = workNewImageBeanDao.queryBuilder().where(WorkNewImageBeanDao.Properties.WorkBaseId.eq(workBaseId)).build().list();
        return imageBean;
    }

    /**
     * 查询所有工序
     *
     */
    public List<WorkNewImageBean> searchAllWorkBeanList() {
        if (workNewImageBeanDao == null) {
            return new ArrayList<>();
        }
        recordBeanDao.detachAll();
        List<WorkNewImageBean> workNewImageList = workNewImageBeanDao.queryBuilder().list();
        return workNewImageList;
    }


    /**
     * 自定义工序
     * 查询目前为止所有添加的图片和视频
     *
     * @return
     */
    public List<CustomRecordListBean> searchAllCustomAndRecordList(String workBaseId) {
        recordListBeanDao.detachAll();
        List<CustomRecordListBean> list = recordListBeanDao.queryBuilder().where(CustomRecordListBeanDao.Properties.WorkBaseId.eq(workBaseId)).build().list();
        return list;
    }

    /**
     * 自定义工序
     * 删除目前为止某一个自定义服务所有添加的图片和视频
     *
     * @return
     */
    public void delectAllWorkBaseIdRecordList(String workBaseId) {
        recordListBeanDao.detachAll();
        String delectWorkBaseIdList = "delect from WORK_BASE_ID where workBaseId=" + workBaseId;
        daoSession.delete(delectWorkBaseIdList);
    }

    /**
     * 自定义工序
     * 删除目前为止某一个自定义服务所有添加的图片和视频
     *
     * @return
     */
    public void delectAllWorCustomAndRecordList(List<CustomRecordListBean> customList) {
        recordListBeanDao.detachAll();
        recordListBeanDao.deleteInTx(customList);
    }

    /**
     * 插入一条自定义工序
     *
     * @param
     */
    public void insertCustomBean(CustomRecordListBean imageBean) {
        recordListBeanDao.detachAll();
        recordListBeanDao.insert(imageBean);
    }

    /**
     * 删除对应的自定义工序
     */
    public void deleteCustomBean(CustomRecordListBean imageBean) {
        recordListBeanDao.detachAll();
        recordListBeanDao.delete(imageBean);
    }

    /**
     * 查询唯一标识下的自定义工序
     */
    /**
     * 自定义工序
     * 通过唯一标识 workBaseId + 自定义工序下标(第一次初始化的下标)
     * <p>
     * 标准工序
     * 通过唯一标识 workBaseId + sn
     *
     * @param
     * @return
     */
    public CustomRecordListBean searchByCustomSn(String sn) {
        recordListBeanDao.detachAll();
        CustomRecordListBean imageBean = recordListBeanDao.queryBuilder().where(CustomRecordListBeanDao.Properties.Sn.eq(sn)).build().unique();
        return imageBean;
    }

    /**
     * 更新对应的自定义工序
     */
    public void updateCustomRecord(CustomRecordListBean listBean) {
        recordListBeanDao.detachAll();
        recordListBeanDao.update(listBean);
    }

    /**
     * 检测图片
     */
    /**
     * 通过唯一标识 workBaseID + system 查询对饮题下的
     *
     * @param
     * @return
     */
    public AloneImageBean searchByImageOnlyLogo(String onlyLogo) {
        imageBeanDao.detachAll();
        AloneImageBean imageBean = imageBeanDao.queryBuilder().where(AloneImageBeanDao.Properties.OnlyLogo.eq(onlyLogo)).build().unique();
        return imageBean;
    }

    /**
     * 通过唯一标识 获取每个检测点的评论
     *
     * @param
     * @return
     */
    public AloneItemCommentBean getAloneItemCommentBean(String commentId) {
        imageBeanDao.detachAll();
        AloneItemCommentBean bean = aloneItemCommentBeanDao.queryBuilder().where(AloneItemCommentBeanDao.Properties.CommentId.eq(commentId)).build().unique();
        return bean;
    }

    /**
     * 插入检测点的评论
     *
     * @param
     */
    public void insertAloneItemCommentBean(AloneItemCommentBean bean) {
        aloneItemCommentBeanDao.detachAll();
        aloneItemCommentBeanDao.insert(bean);
    }

    /**
     * 更新检测点的评论
     *
     * @param
     */
    public void updateAloneItemCommentBean(AloneItemCommentBean bean) {
        aloneItemCommentBeanDao.detachAll();
        aloneItemCommentBeanDao.update(bean);
    }

    /**
     * 插入一条图片和视频的数据
     *
     * @param
     */
    public void insertAloneImageAndRecordBean(AloneImageBean imageBean) {
        imageBeanDao.detachAll();
        imageBeanDao.insert(imageBean);
    }

    /**
     * 查询目前为止所有添加的图片和视频
     *
     * @return
     */
    public List<AloneImageBean> searchAllAloneImageAndRecordList() {
        imageBeanDao.detachAll();
        List<AloneImageBean> list = imageBeanDao.queryBuilder().list();
        return list;
    }

    /**
     * 更新对应的图片数据
     */
    public void updateAloneAndRecord(AloneImageBean listBean) {
        imageBeanDao.detachAll();
        imageBeanDao.update(listBean);
    }

    /**
     * 通过唯一标识 接车单id 查询对应的存储对象
     *
     * @param receiveBaseId
     * @return
     */
    public MineRecordBean searchByReceiveBaseId(long receiveBaseId) {
        recordBeanDao.detachAll();
        MineRecordBean recordBean = recordBeanDao.queryBuilder().where(MineRecordBeanDao.Properties.ReceiveBaseId.eq(receiveBaseId)).build().unique();
        return recordBean;
    }

    /**
     * 插入一条接车单id关联录音文件的数据
     *
     * @param recordBean
     */
    public void insertRecordBean(MineRecordBean recordBean) {
        recordBeanDao.detachAll();
        recordBeanDao.insert(recordBean);
    }

    /**
     * 查询目前为止所有添加的接车单关联录音数据list
     *
     * @return
     */
    public List<MineRecordBean> searchAllRecordBeanList() {
        if (recordBeanDao == null) {
            return new ArrayList<>();
        }
        recordBeanDao.detachAll();
        List<MineRecordBean> recordBeans = recordBeanDao.queryBuilder().list();
        return recordBeans;
    }

    /**
     * 删除对应的接车单关联录音数据list
     */
    public void deleteRecordBean(MineRecordBean listBean) {
        recordBeanDao.detachAll();
        recordBeanDao.delete(listBean);
    }

    /**
     * 更新对应的接车单关联录音数据
     */
    public void updateRecordBean(MineRecordBean listBean) {
        recordBeanDao.detachAll();
        recordBeanDao.update(listBean);
    }



    public void insertCarMemberBean(CarMemberBean carMemberBean){
        carMemberBeanDao.deleteAll();
        carMemberBeanDao.insert(carMemberBean);
    }

    public void updateCarMemberBean(CarMemberBean carMemberBean){
        carMemberBeanDao.detachAll();
        carMemberBeanDao.update(carMemberBean);
    }


    public List<CarMemberBean> searchAllCarMemberBean(){
        if (carMemberBeanDao == null) {
            return new ArrayList<>();
        }
        carMemberBeanDao.detachAll();
        List<CarMemberBean> recordBeans = carMemberBeanDao.queryBuilder().list();
        return recordBeans;
    }
    public CarMemberBean searchCarMemberBean(String carName){
        carMemberBeanDao.detachAll();
        CarMemberBean carMemberBean = carMemberBeanDao.queryBuilder().where(CarMemberBeanDao.Properties.CarName.eq(carName)).build().unique();
        return carMemberBean;
    }



    /**
     * 插入一条推荐服务项目数据
     *
     * @param serviceListBean
     */
//    public void insertAloneService(AloneServiceListBean serviceListBean) {
//        serviceListBeanDao.detachAll();
//        try {
//            serviceListBeanDao.insert(serviceListBean);
//        }catch (Exception e) {
//            Log.e("SSSS","插入数据报错: " + e.toString());
//        }
//
//    }

    /**
     * 通过唯一标识 workBaseId + serviceSn 查询对应的存储对象
     *
     * @param onlyLogo
     * @return
     */
//    public AloneServiceListBean searchByOnlyLogo(String onlyLogo) {
//        serviceListBeanDao.detachAll();
//        AloneServiceListBean serviceListBean = serviceListBeanDao.queryBuilder().where(AloneServiceListBeanDao.Properties.Only_logo.eq(onlyLogo)).build().unique();
//        return serviceListBean;
//    }

    /**
     * 查询目前为止所有添加的推荐服务项目
     *
     * @return
     */
//    public List<AloneServiceListBean> searchAllServiceByWorkBaseIdItem(int workBaseId) {
//        serviceListBeanDao.detachAll();
//        List<AloneServiceListBean> serviceListBeans = serviceListBeanDao.queryBuilder().where(AloneServiceListBeanDao.Properties.WorkBaseId.eq(workBaseId)).list();
//        return serviceListBeans;
//    }
//
//    /**
//     * 删除对应的推荐服务项目
//     */
//    public void deleteServiceItem(AloneServiceListBean listBean) {
//        serviceListBeanDao.detachAll();
//        boolean b = serviceListBeanDao.hasKey(listBean);
//        if (b) {
//            serviceListBeanDao.delete(listBean);
//        }
//    }

//    /**
//     * 删除所有添加的推荐服务项目
//     */
//    public void deleteAllServiceItem() {
//        serviceListBeanDao.detachAll();
//        serviceListBeanDao.deleteAll();
//    }
//
//    /**
//     * 更新对应的推荐服务项目的数据
//     */
//    public void updateServiceItem(AloneServiceListBean listBean) {
//        serviceListBeanDao.detachAll();
//        serviceListBeanDao.update(listBean);
//    }

    /**
     * 插入一条数据
     *
     * @param searchBean
     */
    public void insertAloneSearch(AloneSearchBean searchBean) {
        try {
            aloneSearchBeanDao.detachAll();
            aloneSearchBeanDao.insert(searchBean);
        } catch (Exception e) {
        }
    }

    /**
     * 更改对应用户下的搜索数据
     *
     * @param searchBean
     */
    public void updateAloneSearchBean(AloneSearchBean searchBean) {
        try {
            aloneSearchBeanDao.detachAll();
            aloneSearchBeanDao.update(searchBean);
        } catch (Exception e) {
        }

    }

    /**
     * 根据用户名称 查询对应数据
     *
     * @param name
     * @return
     */
    public AloneSearchBean selectAloneBean(String name) {
        AloneSearchBean saveSearchBean = null;
        try {
            aloneSearchBeanDao.detachAll();
            saveSearchBean = aloneSearchBeanDao.queryBuilder().where(AloneSearchBeanDao.Properties.UserName.eq(name)).build().unique();
        } catch (Exception e) {
        }

        return saveSearchBean;
    }

    /**
     * 删除搜索服务项目数据
     */
    public void deleteAloneSearch() {
        aloneSearchBeanDao.detachAll();
        aloneSearchBeanDao.deleteAll();
    }

    /**
     * 插入一条数据
     *
     * @param searchBean
     */
    public void insertSave(SaveSearchBean searchBean) {
        try {
            searchBeanDao.detachAll();
            searchBeanDao.insert(searchBean);
        } catch (Exception e) {
        }
    }

    /**
     * 根据用户名称 查询对应数据
     *
     * @param name
     * @return
     */
    public SaveSearchBean selectSaveBean(String name) {
        searchBeanDao.detachAll();
        SaveSearchBean saveSearchBean = searchBeanDao.queryBuilder().where(SaveSearchBeanDao.Properties.UserName.eq(name)).build().unique();

        return saveSearchBean;
    }

    /**
     * 删除指定数据
     * @param name
     */
    public void deleteSaveBean(String name) {
        searchBeanDao.detachAll();
        searchBeanDao.delete(selectSaveBean(name));
    }

    /**
     * 更改对应用户下的搜索数据
     *
     * @param searchBean
     */
    public void updateSaveBean(SaveSearchBean searchBean) {
        try {
            searchBeanDao.detachAll();
            searchBeanDao.update(searchBean);
        } catch (Exception e) {
        }

    }

    /**
     * insert() 插入方法调用数据
     */
    public void insertMethodBean(MethodTransferBean bean) {
        try {
                methodTransferBeanDao.detachAll();
                methodTransferBeanDao.insert(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除方法调用数据
     */
    public void deleteMethodBean(MethodTransferBean bean) {
        try {
            try {
                methodTransferBeanDao.detachAll();
                methodTransferBeanDao.delete(bean);
            } catch (Exception e) {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除全部方法调用数据
     */
    public void deleteAllMethodBean() {
        try {
            try {
                methodTransferBeanDao.detachAll();
                methodTransferBeanDao.deleteAll();
            } catch (Exception e) {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *  查询插入方法调用数据
     */
    public List<MethodTransferBean> searchAllMethodBean(String tel) {
        try {
            methodTransferBeanDao.detachAll();
            List<MethodTransferBean> entityBean = methodTransferBeanDao.queryBuilder().where(MethodTransferBeanDao.Properties.Tel.eq(tel)).build().list();
            return entityBean;
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * insert() 插入数据
     */
    public void insertDbBean(AddTagBean bean) {
        try {
            getDaoInstance().insert(bean);
            Log.d("LUO", "插入成功");

        } catch (Exception e) {
            e.printStackTrace();
            Log.d("LUO", "插入本地数据失败：" + e.getMessage());
        }
    }

    /**
     * 查询此标签是否被添加过
     *
     * @param name
     * @return
     */
    public boolean selectTag(String name) {
        getDaoInstance().getAddTagBeanDao().detachAll();
        AddTagBean tagBean = getDaoInstance().getAddTagBeanDao().queryBuilder().where(AddTagBeanDao.Properties.Tag.eq(name)).build().unique();

        return tagBean == null;
    }

    /**
     * 查询所有存储的标签
     *
     * @param
     * @return
     */
    public List<AddTagBean> selectAllTag() {
        getDaoInstance().getAddTagBeanDao().detachAll();
        List<AddTagBean> tagBeans = getDaoInstance().getAddTagBeanDao().queryBuilder().list();

        return tagBeans;
    }

    /**
     * 删除指定标签
     */
    public void deleteOneTag(AddTagBean tagBean) {
        getDaoInstance().getAddTagBeanDao().detachAll();
        getDaoInstance().getAddTagBeanDao().delete(tagBean);
    }

    /**
     * 删除所有tag
     *
     * @param
     * @return
     */
    public void deleteTag() {
        getDaoInstance().getAddTagBeanDao().deleteAll();
    }

    /**
     * insertOrReplace()数据存在则替换，数据不存在则插入
     */
    public void insertOrReplaceDbBean(AddTagBean bean) {
        try {
            getDaoInstance().insertOrReplace(bean);

        } catch (Exception e) {
            e.printStackTrace();
            Log.d("LUO", "插入或替换本地数据失败：" + e.getMessage());
        }

    }

    /**
     * delete()删除单个数据
     */
    public static void deleteDbBean(AddTagBean bean) {
        try {
            getDaoInstance().delete(bean);

        } catch (Exception e) {
            e.printStackTrace();
            Log.d("LUO", "删除本地数据失败：" + e.getMessage());
        }
    }

    /**
     * deleteAll()删除所有数据
     */
    public void deleteAllDbBean(AddTagBean bean) {
        try {
            getDaoInstance().deleteAll(bean.getClass());

        } catch (Exception e) {
            e.printStackTrace();
            Log.d("LUO", "删除本地所有数据失败：" + e.getMessage());
        }

    }

    /**
     * update()修改本地数据
     */
    public void updateDbBean(AddTagBean bean) {
        try {
            getDaoInstance().update(bean);
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("LUO", "修改本地所有数据失败：" + e.getMessage());
        }

    }

    /**
     * loadAll()查询本地所有数据
     */
    public List<AddTagBean> queryAll(AddTagBean bean) {
        List<AddTagBean> beanList = null;
        try {
            beanList = (List<AddTagBean>) getDaoInstance().loadAll(bean.getClass());
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("LUO", "查询本地所有数据失败：" + e.getMessage());
        }

        return beanList;
    }

    /**
     * 根据原始 SQL 数据查询
     * 手输写 SQL 语句sqlConditions
     */
    public List<? extends AddTagBean> querySqlAll(AddTagBean bean, String sqlConditions) throws ClassCastException {
        List<AddTagBean> beanList = null;
        try {
            //查询条件
            WhereCondition.StringCondition stringCondition = new WhereCondition.StringCondition(sqlConditions);
            //查询QueryBuilder
            QueryBuilder<AddTagBean> queryBuilder = (QueryBuilder<AddTagBean>) getDaoInstance().queryBuilder(bean.getClass());
            //添加查询条件
            queryBuilder.where(stringCondition);

            beanList = queryBuilder.build().list();
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("LUO", "sql按条件查询本地数据失败：" + e.getMessage());
        }

        return beanList;
    }
}
